home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / fragment.zip / FRAGMENT.C < prev    next >
C/C++ Source or Header  |  1988-01-21  |  7KB  |  232 lines

  1. /*
  2.  * fragment.c  -- program to break BIG programs up into <360Kb files
  3.  *                   so that they can read into/out of PC's that only
  4.  *                   have 360Kb drives.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. long filelength(int) ;            /* Wasn't declared in <stdio.h> ! */
  11.  
  12. #define    TRUE    1
  13. #define FALSE    0
  14.  
  15. #define    DISK_SIZE_DESIRED    327680L
  16. #define CHUNK_SIZE        16384
  17. #define    MAX_FNAME_LEN        256
  18.  
  19. char buffer[CHUNK_SIZE] ;
  20. int prompt = FALSE ;
  21. char out_path[MAX_FNAME_LEN+1] ;
  22. char out_template[MAX_FNAME_LEN+1] ;
  23.  
  24. void prompt_for_new_file(fragment_nbr)
  25. int fragment_nbr ;
  26. {
  27.     fprintf(stderr, "Strike any key when ready for Fragment %0d.", fragment_nbr) ;
  28.     fflush(stderr) ;
  29.     fflush(stdin) ;
  30.     getch() ;
  31. }
  32.  
  33. FILE *next_output_file(out_template, suffix_ndx, fragment_nbr)
  34. char *out_template ;
  35. int suffix_ndx ;
  36. int fragment_nbr ;
  37. {
  38.     FILE *out_stream ;
  39.     char suffix[3+1] ;
  40.  
  41.     sprintf(suffix, "%0d", 100+fragment_nbr) ;
  42.     strncpy(&out_template[suffix_ndx], &suffix[1], 2) ;
  43.  
  44.     if ((out_stream = fopen(out_template, "rb")) != NULL) {
  45.     fprintf(stderr, "The output file '%s' already exists.\n", out_template) ;
  46.     fprintf(stderr, "   Hit Ctrl-Break to abort, any other key to overwrite.\n") ;
  47.     getch() ;
  48.     fclose(out_stream) ;
  49.     }
  50.     if ((out_stream = fopen(out_template, "wb")) == NULL) {
  51.     fprintf(stderr, "can't open %s for output.\n", out_template) ;
  52.     exit(1) ;
  53.     }
  54. }
  55.  
  56. void fragment(big_stream, out_template, suffix_ndx, fragment_cnt)
  57. FILE *big_stream ;
  58. char *out_template ;
  59. int   suffix_ndx ;
  60. int   fragment_cnt ;
  61. {
  62.     long  bytes_written ;
  63.     FILE *out_stream ;
  64.     int   byte_cnt ;
  65.     int   fragment_nbr ;
  66.     int   chunk ;
  67.  
  68.     bytes_written = 0L ;
  69.     for (fragment_nbr=0; fragment_nbr < fragment_cnt; fragment_nbr++) {
  70.  
  71.     if (prompt)
  72.         prompt_for_new_file(fragment_nbr) ;
  73.  
  74.     out_stream = next_output_file(out_template, suffix_ndx, fragment_nbr) ;
  75.  
  76.     for (chunk=0; chunk < DISK_SIZE_DESIRED/CHUNK_SIZE; chunk++) {
  77.         byte_cnt = fread(buffer, 1, CHUNK_SIZE, big_stream) ;
  78.         if (byte_cnt == 0)
  79.         break ;
  80.         fprintf(stderr, "\rFragment %2d, Chunk %2d: %0d bytes Read     ",
  81.             fragment_nbr, chunk, byte_cnt) ;
  82.         fflush(stderr) ;
  83.         if (fwrite(buffer, 1, byte_cnt, out_stream) != byte_cnt) {
  84.         fprintf(stderr, "\n") ;
  85.         fprintf(stderr, "Error Writing Fragment %2d, Chunk %2d.\n") ;
  86.         fprintf(stderr, "Total of %0ld were successfully coalesced.\n",
  87.                 bytes_written) ;
  88.         exit(1) ;
  89.         }
  90.         bytes_written += (long)byte_cnt ;
  91.         fprintf(stderr, "\rFragment %2d, Chunk %2d: %0d bytes Written    ",
  92.             fragment_nbr, chunk, byte_cnt) ;
  93.         fflush(stderr) ;
  94.     }
  95.  
  96.     fclose(out_stream) ;
  97.     fprintf(stderr, "\rFragment %2d: Completed                                     \n", fragment_nbr) ;
  98.     }
  99.  
  100.     fclose(big_stream) ;
  101.     fprintf(stderr, "End of Input File successfully reached.  Fragmentation complete.\n") ;
  102. }
  103.  
  104. void get_template(file_template, out_template, suffix_ndx)
  105. char *file_template ;
  106. char *out_template ;
  107. int  *suffix_ndx ;
  108. {
  109.     char fname_template[8+1] ;
  110.     char file_ext[1+3+1] ;
  111.     char *file_name ;
  112.     char *file_dot ;
  113.  
  114.     *out_path = '\0' ;
  115.     file_name = file_template ;
  116.     if (strchr(file_template, '\\') || strchr(file_template, ':')) {
  117.     if (strchr(file_template, '\\'))
  118.         file_name = strrchr(file_template, '\\')+1 ;
  119.         else
  120.         file_name = strrchr(file_template, ':')+1 ;
  121.     if (file_name-file_template > MAX_FNAME_LEN) {
  122.         fprintf(stderr, "Directory path of %s is too long.\n", file_template) ;
  123.         exit(1) ;
  124.     } else {
  125.         strncpy(out_path, file_template, file_name-file_template) ;
  126.         out_path[file_name-file_template+1] = '\0' ;
  127.     }
  128.     }
  129.  
  130.     *file_ext = '\0' ;
  131.     file_dot = strchr(file_name, '.') ;
  132.     if (file_dot)
  133.     if (strlen(file_dot) > 4) {
  134.         fprintf(stderr, "The extension of '%s' is too long.\n", file_dot) ;
  135.         exit(1) ;
  136.     } else {
  137.         strcpy(file_ext, file_dot) ;
  138.         *file_dot = '\0' ;
  139.     }
  140.  
  141.     strcpy(fname_template, "________") ;
  142.     if (strlen(file_name) > 6) {
  143.     fprintf(stderr, "The filename of '%s' is too long.\n", file_name) ;
  144.     exit(1) ;
  145.     }
  146.     strncpy(fname_template, file_name, strlen(file_name)) ;
  147.  
  148.     if (strlen(out_path)+strlen(fname_template)+strlen(file_ext) > MAX_FNAME_LEN) {
  149.     fprintf("The resulting output pathname of '%s%s%s' is too long.\n",
  150.            out_path, fname_template,file_ext) ;
  151.     exit(1) ;
  152.     }
  153.  
  154.     strcpy(out_template, out_path) ;
  155.     strcat(out_template, fname_template) ;
  156.     *suffix_ndx = strlen(out_template) - 2 ;
  157.     strcat(out_template, file_ext) ;
  158. }
  159.  
  160. void main(argc, argv)
  161. int argc ;
  162. char *argv[] ;
  163. {
  164.     FILE *big_stream ;
  165.     long  length ;
  166.     int   fragment_cnt ;
  167.     int   suffix_ndx ;
  168.  
  169.     argc-- ; /* throw away "program name" argument */
  170.     argv++ ;
  171.  
  172.     /* parse off any "prompt option" argument that is present */
  173.  
  174.     prompt = FALSE ;
  175.     if (argc && (stricmp(argv[0], "-p") == 0)) {
  176.     argc-- ;
  177.     argv++ ;
  178.     prompt = TRUE ;
  179.     }
  180.  
  181.     if ((argc != 2) || (*argv[0] == '?')) {
  182.     fprintf(stderr, "\n") ;
  183.     fprintf(stderr, "   fragment [-p] <big fname> <output file template>\n") ;
  184.     fprintf(stderr, "\n") ;
  185.     fprintf(stderr, "   Where:\n") ;
  186.     fprintf(stderr, "\n") ;
  187.     fprintf(stderr, "       -p optionally causes the program to pause between output files.\n") ;
  188.     fprintf(stderr, "           (allowing you to, say, change output disks)\n") ;
  189.     fprintf(stderr, "       <big fname> is the full pathname to the file to be fragmented.\n") ;
  190.     fprintf(stderr, "       <output file template> is the path to the output directory\n") ;
  191.     fprintf(stderr, "          along with a six letter filename template which will\n") ;
  192.     fprintf(stderr, "          be suffixed with the fragment nbr and the extension.\n") ;
  193.     fprintf(stderr, "\n") ;
  194.     fprintf(stderr, "   Example:\n") ;
  195.     fprintf(stderr, "\n") ;
  196.     fprintf(stderr, "       fragment -p c:\\pcip\\src\\doc.tar a:doc.frg\n") ;
  197.     fprintf(stderr, "\n") ;
  198.     fprintf(stderr, "       Causes a fragmented copy of c:\\pcip\\src\\doc.tar\n") ;
  199.     fprintf(stderr, "          to be written to a:doc___00.frg, a:doc___01.frg, etc.\n") ;
  200.     fprintf(stderr, "       Since -p is present, the program will pause between each file.\n") ;
  201.     exit(1) ;
  202.     }
  203.  
  204.     if ((big_stream = fopen(argv[0], "rb")) == NULL) {
  205.     fprintf(stderr, "can't open %s for input.\n", argv[0]) ;
  206.     exit(1) ;
  207.     }
  208.  
  209.     length = filelength(fileno(big_stream)) ;
  210.     if (length == -1L) {
  211.     fprintf(stderr, "filelength on %s failed.\n", argv[0]) ;
  212.     fclose(big_stream) ;
  213.     exit(1) ;
  214.     }
  215.  
  216.     fragment_cnt = (length + DISK_SIZE_DESIRED - 1L) / DISK_SIZE_DESIRED ;
  217.     if (fragment_cnt <= 1) {
  218.     fprintf(stderr, "%s (%ld bytes) should already fit onto one disk.\n", argv[0], length) ;
  219.     fprintf(stderr, "   disk size is considered to be >= %ld\n", DISK_SIZE_DESIRED) ;
  220.     fclose(big_stream) ;
  221.     exit(1) ;
  222.     }
  223.  
  224.     get_template(argv[1], out_template, &suffix_ndx) ;
  225.  
  226.     fprintf(stderr, "Fragmentation will result in the creation of %d Fragments\n", fragment_cnt) ;
  227.     if (fragment_cnt > 99)
  228.     fprintf(stderr, "Sorry, can't handle more than 100 fragment files.\n") ;
  229.     else
  230.     fragment(big_stream, out_template, suffix_ndx, fragment_cnt) ;
  231. }
  232.